//
//  MineProjectsViewController.swift
//  GitOSC-Swift
//
//  Created by 李萍 on 15/12/30.
//  Copyright © 2015年 李萍. All rights reserved.
//

import UIKit
import Kingfisher

class MineProjectsViewController: UIViewController {
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        let userinfos = UserInfos()
        let private_token = userinfos.getprivateToken()
        if private_token.isEmpty {
            let storyboard = UIStoryboard.init(name:"Main", bundle: nil)
            let loginView : LoginViewController = storyboard.instantiateViewControllerWithIdentifier("login") as! LoginViewController
            
            self.presentViewController(loginView, animated: true, completion: nil)
        } else {
            let imageView = UIImageView.init(frame: CGRectMake(0, 0, 30, 30))
            imageView.layer.cornerRadius = 15
            imageView.clipsToBounds = true
            imageView.userInteractionEnabled = true
            imageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action:"myinfos"))
            
            let portraitURL = NSUserDefaults.standardUserDefaults().valueForKey("portrait") as! String
            
            //添加头像
            imageView.kf_setImageWithURL(NSURL.init(string: "https://git.oschina.net/"+portraitURL)!, placeholderImage: UIImage.init(named: "LoginLogo"))
            self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: imageView)
            
            self.addSubviewsForMine()
        }

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func addSubviewsForMine() {
        let userinfos = UserInfos()
        let userId = userinfos.getUserID()
        let private_token = userinfos.getprivateToken()
        
        let titleView = TitleViewController(titles: ["动态", "项目", "Star", "Watch"],
            controllers: [EventsViewController(privateToken: private_token),
                ProjectsTableViewController(privateToken: private_token),
                ProjectsTableViewController(userID: userId, projectType: 5),
                ProjectsTableViewController(userID: userId, projectType: 6)],
            hasNav: true,
            hasTabbar: true,
            titlesHeight: CGFloat(35),
            buttonWidth: 0,
            footHeight: CGFloat(3))
        
        titleView.buttonBackgroundColor = UIColor.grayColor()
        titleView.buttonSelectedColor = UIColor.blackColor()
        titleView.buttonUnSelectedColor = UIColor.whiteColor()
        
        self.addChildViewController(titleView)
        self.view.addSubview(titleView.view)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func myinfos() {
        
        let storyboard = UIStoryboard.init(name:"Main", bundle: nil)
        let loginView : MyInfosViewController = storyboard.instantiateViewControllerWithIdentifier("myinfos") as! MyInfosViewController
        self.navigationController?.pushViewController(loginView, animated: true)
    }

    
}
